package com.shaoshaohuo.app.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Message;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.ui.OrderDetailActivity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageListAdapter<T> extends MyBaseAdapter<Message> {

	private boolean isMessage = true;

	public MessageListAdapter(Context ctx, List<Message> list, boolean flag) {
		super(ctx, list, flag);
	}

	public void setMessage(boolean isMessage) {
		this.isMessage = isMessage;
	}

	// @Override
	// public int getViewTypeCount() {
	// return 2;
	// }
	//
	// @Override
	// public int getItemViewType(int position) {
	// if (isMessage) {
	// return 1;
	// } else {
	// return 2;
	// }
	// }

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			// if (getItemViewType(pos) == 1) {
			convertView = View.inflate(context, R.layout.view_message_list_item, null);
			holder.timeText = (TextView) convertView.findViewById(R.id.textview_time);
			holder.contentText = (TextView) convertView.findViewById(R.id.textview_content);
			holder.headImage = (ImageView) convertView.findViewById(R.id.imageview_logo);
			holder.detailText = (TextView) convertView.findViewById(R.id.textview_detail);
			// } else {
			// convertView = View.inflate(context,
			// R.layout.view_activity_list_item, null);
			holder.mActivityImage = (ImageView) convertView.findViewById(R.id.imageview_image);
			holder.messageLayout = convertView.findViewById(R.id.layout_message);
			// }
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Message item = list.get(pos);
		if (isMessage) {
			holder.messageLayout.setVisibility(View.VISIBLE);
			holder.mActivityImage.setVisibility(View.GONE);
			holder.timeText.setText(StringHelper.formatDateMinute(item.getCursor()));
			holder.contentText.setText(item.getContent());
			// LoadUtils.getInstance().loadImage(holder.headImage,
			// item.getAvatar(), 0, 90);
			ImageLoader.getInstance().displayImage(item.getAvatar(), holder.headImage,
					ImageLoadUtil.getDisplayImageOptions(R.drawable.icon_default_headimage, 180));
			holder.detailText.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, OrderDetailActivity.class);
					intent.putExtra(ExtraName.orderid, item.getOrderid());
					context.startActivity(intent);
				}
			});
		} else {
			holder.messageLayout.setVisibility(View.GONE);
			holder.mActivityImage.setVisibility(View.VISIBLE);

			ImageLoader.getInstance().displayImage(item.getImg(), holder.mActivityImage, ImageLoadUtil.build);
			// LoadUtils.getInstance().loadImage(holder.mActivityImage,
			// item.getImg());
		}

		return convertView;
	}

	static class ViewHolder {
		TextView timeText;
		TextView contentText;
		ImageView headImage;
		TextView detailText;

		View messageLayout;

		ImageView mActivityImage;
	}
}
